package beans;
//jsp+javabean+Servelet的类
public class Complex {
    private double real;
    private double ima;
    public Complex(){}
    public Complex(double r, double i){
        this.real=r;
        this.ima=i;
    }

    public double getReal() {
        return real;
    }

    public void setReal(double real) {
        this.real = real;
    }

    public double getIma() {
        return ima;
    }

    public void setIma(double ima) {
        this.ima = ima;
    }

    public Complex add(Complex x){
        return new Complex(this.real+x.real,this.ima+x.ima);
    }
    public Complex sub(Complex x){
        return new Complex(this.real-x.real,this.ima-x.ima);
    }
    public Complex mul(Complex x){
        return new Complex(this.real*x.real-this.ima*x.ima,this.ima*x.real+this.real*x.ima);
    }
    public Complex div(Complex x){
        double m=this.ima*this.ima+x.ima*x.ima;
        return new Complex((this.real*x.real+this.ima*x.ima)/m,(this.ima*x.real-this.real*x.ima)/m);
    }

    public String info(){
        if(ima>=0) return real+"+"+ima+"*i";
        else return real+"-"+(-ima)+"*i";
    }
}
